#!/usr/bin/python
#-*- coding:utf-8 -*-
# Author : yh_zhu
# Description: expand include filelist into one file, remove recursive file including
#     Usage : build_inc_lst <inc_file>
#             make sure that there are no comments in <inc_file> before using this command
#
################################################################
import re, os


def is_include(f):
    n, ext = os.path.splitext(f)
    if ext == '.inc' :
        return True
    else :
        return False


def expand(f):
    inc = re.compile(r'\s*`include\s+(\S+)')
    s = ''
    with open(f) as f:
        for line in f :
            m = re.match(inc, line)
            if m :
                fname = m.group(1).replace('"', '').strip()
                if is_include(fname) and os.path.exists(fname) :
                    s += '\n//########### <begin_expand> ' + fname + '\n'
                    s += expand(fname)
                    s += '\n//########### <end_expand/> ' + fname + '\n\n'
                else :
                    s += line
            else :
                s += line
    return s

if __name__ == '__main__':
    import sys
    print expand(sys.argv[1])
